<template>
  <div class="app-container">
    <el-form ref="placeCodeForm" v-loading="formLoading" :rules="rules" :model="form" label-position="top">
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="一级分类" prop="type" style="width: 400px">
            <el-cascader
              v-model="form.classOne"
              clearable
              placeholder="请选择一级分类"
              :options="classOneList"
              :props="{value:'postCode',label:'jobTitle'}"
              @change="getFindJobNameTwo"
              filterable></el-cascader>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="二级分类" prop="type" style="width: 400px">
            <el-cascader
              v-model="form.classTwo"
              clearable
              placeholder="请选择二级分类"
              :options="classTwoList"
              :props="{value:'postCode',label:'jobTitle'}"
              filterable></el-cascader>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="分类编号" prop="postCode" style="width: 400px">
            <el-input v-if="id===null" v-model="form.postCode" placeholder="请输入分类编号" clearable/>
            <el-input v-if="id" v-model="form.postCode" placeholder="请输入分类编号" :disabled="true"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="分类名称" prop="title" style="width: 400px">
            <el-input v-model="form.title" placeholder="请输入分类名称" clearable/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {addFindJobNameApi, getFindJobNameByIdApi, getFindJobNameOneApi, getFindJobNameTwoApi} from "@/api/companyCode";
import {bakGetJobFairByIdApi} from "@/api/jobFair";

export default {
  name: 'PlaceCodeDetail',
  data() {
    return {
      classOneList: [],
      classTwoList: [],
      form: {
        id: null,
        classOne: '',
        classTwo: '',
        title: '',
        postCode: ''
      },
      rules: {
        title: [{required: true, trigger: 'blur', message: '请输入分类名称'}],
        postCode: [{required: true, trigger: 'blur', message: '请输入分类编号'}]
      },
      formLoading: false,
    }
  },
  props: ['id', 'closeDialog'],
  methods: {
    submit() {
      this.$refs.placeCodeForm.validate(valid => {
        if (valid) {
          const loading = this.$loading({
            text: '提交中'
          })
          let classOne = '';
          let classTwo = '';
          if(this.form.classOne)
          {
            classOne = this.form.classOne[0]
          }
          if(this.form.classTwo)
          {
            classTwo = this.form.classTwo[0]
          }
          addFindJobNameApi({
            id: this.id,
            classOne: classOne,
            classTwo: classTwo,
            title: this.form.title,
            postCode: this.form.postCode
          }).then((res) => {
            this.$message.success('操作成功');
            this.closeDialog(0);
          }).catch(() => {
          }).finally(() => {
            loading.close();
          })
        } else {
          return false
        }
      })
    },
    getFindJobNameOne() {
      getFindJobNameOneApi().then((res) => {
        this.classOneList = res
      }).finally(() => {
        this.placeTypeLoading = false
      })

    },
    getFindJobNameTwo(value) {
      console.log(value)
      let code = ''
      if(value)
      {
        code = value[0]
      }else {
        code = this.form.classOne
      }
      getFindJobNameTwoApi({fatherCode: code}).then((res) => {
        this.classTwoList = res

      }).finally(() => {
        this.placeTypeLoading = false
      })

    },
    getDetail() {
      this.formLoading = true;
      getFindJobNameByIdApi({id: this.id}).then((res) => {
        this.form = res;
        this.form.title = res.jobTitle
        this.form.classOne = res.classOne ?? ''
        if (this.id) {
          this.getFindJobNameTwo();
        }

      }).finally(() => {
        this.formLoading = false;
      })
    }


  },
  mounted() {
    if (this.id) {
      this.getDetail();
    }
    this.getFindJobNameOne()

  },
}
</script>
<style scoped>
/*
*让select撑开
*/
.el-select, .el-cascader {
  display: block;
}
</style>
